package org.jeecg.modules.demo.ch.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.demo.ch.entity.ChDemandFeedback;
import org.jeecg.modules.demo.ch.entity.RequirementComments;
import org.jeecg.modules.demo.ch.exception.Assert;
import org.jeecg.modules.demo.ch.exception.CustomException;
import org.jeecg.modules.demo.ch.mapper.ChDemandFeedbackMapper;
import org.jeecg.modules.demo.ch.mapper.RequirementCommentsMapper;
import org.jeecg.modules.demo.ch.service.IChDemandFeedbackService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * @Description: 需求反馈
 * @Author: jeecg-boot
 * @Date: 2022-11-15
 * @Version: V1.0
 */
@Service
public class ChDemandFeedbackServiceImpl extends ServiceImpl<ChDemandFeedbackMapper, ChDemandFeedback> implements IChDemandFeedbackService {

    @Resource
    private ChDemandFeedbackMapper chDemandFeedbackMapper;
    @Resource
    private RequirementCommentsMapper requirementCommentsMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveMain(ChDemandFeedback chDemandFeedback, List<RequirementComments> requirementCommentsList) {
        chDemandFeedbackMapper.insert(chDemandFeedback);
        if (requirementCommentsList != null && requirementCommentsList.size() > 0) {
            for (RequirementComments entity : requirementCommentsList) {
                //外键设置
                entity.setChDemandFeedbackId(chDemandFeedback.getId());
                entity.setCommentTime(new Date());//设置评论时间
                requirementCommentsMapper.insert(entity);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMain(ChDemandFeedback chDemandFeedback, List<RequirementComments> requirementCommentsList) {
        chDemandFeedbackMapper.updateById(chDemandFeedback);
        //1.更新子表数据
        if (requirementCommentsList != null && requirementCommentsList.size() > 0) {
            for (RequirementComments entity : requirementCommentsList) {
                //判断新增还是编辑
                LambdaQueryWrapper<RequirementComments> lambdaQueryWrapper = new QueryWrapper<RequirementComments>().lambda()
                        .eq(RequirementComments::getId, entity.getId());
                List<RequirementComments> requirementCommentsLists = requirementCommentsMapper.selectList(lambdaQueryWrapper);
                if (!CollectionUtils.isEmpty(requirementCommentsLists)) {//编辑
                    validateRequirementComments(entity);//校验子表并设置填充内容
                    requirementCommentsMapper.updateById(entity);
                } else {//新增
                    //外键设置
                    validateRequirementComments(entity);//校验子表并设置填充内容
                    entity.setChDemandFeedbackId(chDemandFeedback.getId());
                    requirementCommentsMapper.insert(entity);
                }
            }
        }
//		//1.先删除子表数据  拿到子表的所有数据（旧的）
//		requirementCommentsMapper.deleteByMainId(chDemandFeedback.getId());
//
//		//2.子表数据重新插入 （多一个跟旧数据对比的操作 id存在 就更新实体  不存在重新插入）
//		if(requirementCommentsList!=null && requirementCommentsList.size()>0) {
//			for(RequirementComments entity:requirementCommentsList) {
//				//外键设置
//				entity.setChDemandFeedbackId(chDemandFeedback.getId());
//				requirementCommentsMapper.insert(entity);
//			}
//		}
    }

    /**
     * 校验并填充子表内容
     *
     * @param entity
     */
    public void validateRequirementComments(RequirementComments entity) {
        //数据非空校验
        try {
            Assert.validateEntity(entity);
            String feedbackContent = entity.getFeedbackContent();//反馈内容
            Date feedbackTime = entity.getFeedbackTime();//反馈时间
            if (StringUtils.hasLength(feedbackContent) && feedbackTime == null) {
                entity.setFeedbackTime(new Date());//设置反馈时间
            }
            String comments = entity.getComments();//评论内容
            Date commentTime = entity.getCommentTime();//评论时间
            if (StringUtils.hasLength(comments) && commentTime == null) {
                entity.setCommentTime(new Date());//设置评论时间
            }
        } catch (CustomException e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delMain(String id) {
        requirementCommentsMapper.deleteByMainId(id);
        chDemandFeedbackMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delBatchMain(Collection<? extends Serializable> idList) {
        for (Serializable id : idList) {
            requirementCommentsMapper.deleteByMainId(id.toString());
            chDemandFeedbackMapper.deleteById(id);
        }
    }

}
